<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\health\model\HealthPopularize;
use app\health\model\tracking\HealthTrackingUrl;
use app\health\model\tracking\HealthTrackingUrlCode;
use app\health\service\WeMiniService;
use think\Cache;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout      = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function jump_mp()
    {
        $zfCode   = trim($this->request->param('zfCode', ''));
        $mobile   = $this->request->param('mobile', '');
        $pid      = $this->request->param('pid', 0);
        $site_id  = $this->request->param('site_id', 0);
        $platform = $this->request->param('platform', '');
        $ip       = $this->request->ip();
        $pack     = $this->request->header('x-requested-with', '');
        $pathType = (int)$this->request->param('path_type', '0');
        if ($this->request->isAjax()) {
            $channelId = $this->request->param('channel_id', 0);
            $platform  = $this->request->param('platform', '');
            $query     = [];
            if ($channelId) {
                $query['channel_id'] = $channelId;
            }
            if ($platform) {
                $query['platform'] = $platform;
            }
            if ($pathType === 1) {
                $query['type'] = 'week';
            }
            $query = http_build_query($query);
            $path  = ['/pages/main/index/index',
                      '/pages/user/receive/index',
                      '/pages/user/equityCard/index'][$pathType] ?? '/pages/main/index/index';
            $data  = WeMiniService::createUrlLink([
                'path'        => $path,
                'query'       => $query,
                'expire_type' => 0,
                'expire_time' => strtotime('+30 day'),
            ]);
            return json(['wxmini_url' => $data['url_link']]);
        }
        $channelId = 0;
        if ($zfCode) {
            $popularize = HealthPopularize::where('zf_code', $zfCode)->where('pid', $pid)->find();
            if (!$popularize) {
                $popularize = HealthPopularize::create([
                    'type'         => 1,
                    'channel_type' => 'h5',
                    'channel_code' => _random_str(6),
                    'pid'          => $pid,
                    'zf_code'      => $zfCode,
                    'extend'       => json_encode(['site_id' => $site_id]),
                ]);
            }
            $channelId = $popularize['id'];
        }
        if ($zfCode && $this->request->ip() != '127.0.0.1' && !Cache::has('tracking:url:' . md5($zfCode . $ip))) {
            //记录请求次数
            HealthTrackingUrl::create([
                'code'     => $zfCode,
                'mobile'   => $mobile,
                'pid'      => $pid,
                'site_id'  => $site_id,
                'platform' => $platform,
                'pack'     => $pack,
                'ip'       => request()->ip(),
                'headers'  => json_encode(request()->header(), JSON_UNESCAPED_UNICODE),
            ]);
            $row = HealthTrackingUrlCode::where('code', $zfCode)->where('day', date('Y-m-d'))->find();
            if (!$row) {
                $row = HealthTrackingUrlCode::create(['code' => $zfCode, 'click_num' => 1, 'day' => date('Y-m-d')]);
            } else {
                $row->setInc('click_num');
            }
            Cache::set('tracking:url:' . md5($zfCode . $ip), 1, 10);
        }
        $this->assign('platform', $platform);
        $this->assign('path_type', $pathType);
        $this->assign('pid', $pid);
        $this->assign('channel_id', $channelId);
        $this->assign('urlKey', 'wx_mini_url_v3:' . md5('wx_mini_url_v3' . $platform . $pid . $channelId . $pathType));
        return $this->fetch();
    }
}
